<?php
  require_once('./db.php');

//pr(get_defined_vars());exit;

 // pr($_SESSION['secretword']);

if(isset($_POST['email']) && !empty($_POST['email']))
{


  ####################################
  $flag = false;

# file upload start
if(isset($_FILES['avatar']['name']) &&
($_SESSION['secretword'] == $_POST['captcha']))
{
  pr($_FILES);

  $errmsg = array();

  $fileDetails = pathinfo($_FILES['avatar']['name']);
  $fileExtn = $fileDetails['extension'];

  $upload_dir = './uploads';
  $thumb_dir = './uploads/thumb';
  if(!in_array($fileExtn, $validExtn)) {
    $flag = false;
    $valid = implode(',',$validExtn);
    $errmsg[] = "Please upload file with valid($valid) extn.";
  }

  # Check For Size
  if(($_FILES['avatar']['size']>$validSize) ||
      ($_FILES['avatar']['error'] != 0 )

  ){
    $flag = false;
    $errmsg[] = "Please upload file with $mb MB Max.";
  } else {

    if(is_uploaded_file($_FILES['avatar']['tmp_name'])){

      # If uploads dir didn't exists then it
      # will try to create it
      if(!is_dir($upload_dir)) {
        mkdir($upload_dir,0755);
      }
      if(!is_dir($thumb_dir)) {
          mkdir($thumb_dir,0755);
      }

      $src = $_FILES['avatar']['tmp_name'];
      $fileName = date('ymdHis').'_'.uniqid().'_'.$_FILES['avatar']['name'];

      $dest = "$upload_dir/$fileName";
      $isUploaded = move_uploaded_file($src,$dest);
      pr($isUploaded);
      if($isUploaded == true){
        $flag = true;
        $_POST['avatar'] = $fileName;
        $errmsg[] = "File uploaded successfully.";

######### CREATE THUMBNAIL  ##########
require_once('thumbnail/image.class.php');
$img = new Zubrag_image;

$img->max_x        = 100;
$img->max_y        = 100;

$img->GenerateThumbFile($dest, "$thumb_dir/$fileName");

###################

      } else {
        $flag = false;
        $errmsg[] = "Please check permission of your uploads directory.";
      }
    }
  }
}
  ####################################
  $_POST = array_map('trim',$_POST);
  $_POST = array_map('strip_tags',$_POST);
  $_POST = array_map('mysql_real_escape_string',$_POST);

  if( ($_SESSION['secretword'] == $_POST['captcha']) && $flag == true)
  {
    unset($_POST['captcha']);

    $_POST['added_date'] = date('Y-m-d H:i:s');
    $_POST['status'] = '1';

    pr($_POST);
    $query = "INSERT INTO `users` SET ";
    $tmp = '';
    foreach ($_POST as $colName => $value) {
      $tmp .= "`$colName`='$value' ,";
    }
    $query = $query.$tmp;
    $query = rtrim($query," ,");
    print_q($query);
    mysql_query($query)or die(mysql_error());
    $id = mysql_insert_id();
    echo "Record inserted on Id :$id";
  } else {
    $flag = false;
    $errmsg[] = "Invalid security code.";
  }
}

# For success / error messages
if(!empty($errmsg)){
  $class = $flag == true ? 'success':'error';
  echo '<ol class="'.$class.'">';
  foreach($errmsg as $msg){
    echo "<li style=\"list-style: none;\">$msg</li>";
  }
  echo '</ol>';
}



?>
<form action="" enctype="multipart/form-data"  method="post">
  <table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">::Please add user::</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">User Name :</td>
        <td ><input required type="text" name="uname" placeholder="My Name is.."/></td>
      </tr>
      <tr>
        <td align="right">Email :</td>
        <td ><input required type="email" name="email" placeholder="My Email is.."/></td>
      </tr>

      <tr>
        <td align="right">Password :</td>
        <td ><input required type="password" name="password" placeholder="My Password is.."/></td>
      </tr>

      <tr>
        <td align="right">Avatar :<br>(<?=implode(',',$validExtn)?>)</td>
        <td ><input required type="file" name="avatar"/></td>
      </tr>

      <tr>
        <td align="right">Secret Word :</td>
        <td >

<!-- Captcha Start -->
<img src="captcha/captcha.php" id="captcha" /><br/>
<a href="javascript:void(0)" onclick="    document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>
<input type="text" name="captcha" id="captcha-form" /><br/>
<!-- Captcha End-->

</td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td ><input type="submit" value="I am done!!" /></td>
      </tr>
    </tbody>
  </table>
</form>
<style type="text/css">
  .success{
    background:lightgreen;
    border:1px solid green;
    padding:10px;
  }
  .error{
    background:pink;
    border:1px solid red;
    padding:10px;
  }

</style>
